package model

import (
	"fmt"
	"reflect"

	"github.com/go-playground/validator/v10"
	"gorm.io/plugin/soft_delete"
)

type Model struct {
	CreateAt int `gorm:"column:create_time;comment:创建时间;autoCreateTime;<-:create;type:int" json:"create_time"`
	UpdateAt int `gorm:"column:update_time;comment:最后更新时间;autoUpdateTime;autoCreateTime;type:int" json:"update_time"`
	DeleteAt int `gorm:"column:delete_time;comment:删除时间;default:null;type:int" json:"delete_time"`
}

type SoftModel struct {
	Model
	DeleteAt *soft_delete.DeletedAt `gorm:"column:delete_time;comment:删除时间;default:null;type:int" json:"delete_time"`
}

func (m *Model) errorMsg(me interface{}, d map[string]interface{}) string {
	var msg string
	switch e := me.(type) {
	case validator.ValidationErrors:
		for _, v := range e {
			for k2, v2 := range d {
				if v.StructField() == k2 {
					switch i := v2.(type) {
					case string:
						msg = v2.(string)
						goto breakHere
					case map[string]string:
						for m, j := range i {
							if v.ActualTag() == m {
								msg = j
								goto breakHere
							}
						}
					}
				}
			}
		}

		msg = e.Error()
	}
breakHere:
	return msg
}

func Hid(old interface{}) {
	s := reflect.ValueOf(old)
	fmt.Println(s)
}
